//
//  JWSettingCalendarNameViewController.m
//  SiqiCalendar
//
//  Created by sunjiawen on 2018/4/10.
//  Copyright © 2018年 sjw. All rights reserved.
//

#import "JWSettingCalendarNameViewController.h"
#import "JWBorderTextField.h"
#import "JWCalendarSettingHelper.h"

@interface JWSettingCalendarNameViewController ()

@property (weak, nonatomic) IBOutlet JWBorderTextField *calendarNameTextField;

@end

@implementation JWSettingCalendarNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.calendarNameTextField resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - Private
/**
 配置UI
 */
- (void)setupUI {
    UIBarButtonItem *nextStepItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextStepItemAction:)];
    
    NSMutableArray *barButtonItems = [NSMutableArray array];
    
    if ([JWCalendarSettingHelper sharedInstance].isEdit) {
        // 编辑状态
        self.title = @"编辑日历";
        
        UIBarButtonItem *doneStepItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(confirmEditItemAction:)];
        
        [barButtonItems addObject:nextStepItem];
        [barButtonItems addObject:doneStepItem];
    }else {
        self.title = @"创建日历";
        
        [barButtonItems addObject:nextStepItem];
    }
    
    self.navigationItem.rightBarButtonItems = barButtonItems;
}

#pragma mark - Actions
- (void)nextStepItemAction:(id)sender {
    [self.calendarNameTextField resignFirstResponder];
    
    [[JWCalendarSettingHelper sharedInstance] updateCalendarName:self.calendarNameTextField.text];
}

/**
 完成编辑-仅修改名字

 @param sender 完成按钮
 */
- (void)confirmEditItemAction:(id)sender {
    [self.calendarNameTextField resignFirstResponder];
    
    [[JWCalendarSettingHelper sharedInstance] updateCalendarName:self.calendarNameTextField.text];
    
    [[JWCalendarSettingHelper sharedInstance] updateRealmData];
}

#pragma mark - Getter
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
